package com.foreveross.weixin.service;

import java.net.URLEncoder;
import java.util.Random;

import org.apache.commons.lang.math.RandomUtils;

import com.foreveross.util.JSONUtil;
import com.foreveross.weixin.common.Constant;
import com.foreveross.weixin.util.WeixinUtil;
import com.foreveross.weixin.vo.AccessToken;
import com.foreveross.weixin.vo.UserInfo;
import com.foreveross.weixin.vo.Qrcode.ActionName;
import com.foreveross.weixin.vo.Qrcode.QrcodeTicket;
import com.foreveross.weixin.vo.Qrcode.QrcodeTicketScene;
import com.foreveross.weixin.vo.Qrcode.QrcodeTicketSceneVo;
import com.foreveross.weixin.vo.Qrcode.QrcodeTicketVo;
import com.foreveross.weixin.vo.Qrcode.ReturnQrcodeVo;

public class QrcodeTicketService {
	
	//临时生成二维码有效时间
	private static int expireSeconds = 604800;
	
	private static int scene_id = 10000;

	public static ReturnQrcodeVo getQrcodeTicket(Object obj){
		
    	QrcodeTicketVo qrcodeTicketVo = new QrcodeTicketVo();
    	qrcodeTicketVo.setExpire_seconds(expireSeconds);
    	qrcodeTicketVo.setAction_name(ActionName.QR_SCENE.getValue());
    	QrcodeTicketScene qrcodeTicketScene = new QrcodeTicketScene();
    	QrcodeTicketSceneVo qrcodeTicketSceneVo = new QrcodeTicketSceneVo();
    	qrcodeTicketSceneVo.setObj(obj);

    	qrcodeTicketSceneVo.setScene_id(scene_id);
    	qrcodeTicketScene.setScene(qrcodeTicketSceneVo);
    	qrcodeTicketVo.setAction_info(qrcodeTicketScene);
    	String jsonStr = JSONUtil.toJsonWithGson(qrcodeTicketVo);
    	
    	System.out.println("accessToken:"+Constant.accessToken.getAccess_token());
    	System.out.println("jsonStr:"+jsonStr);
    	QrcodeTicket a = WeixinUtil.getQrcodeTicket(Constant.accessToken.getAccess_token(), jsonStr);
    	System.out.println("url:"+a.getUrl());
    	System.out.println("ticket:"+a.getTicket());    	
    	System.out.println("ticket encoder:"+URLEncoder.encode(a.getTicket()));
    	
    	ReturnQrcodeVo returnQrcodeVo = new ReturnQrcodeVo();
    	returnQrcodeVo.setSendRandomId(obj.toString());
    	returnQrcodeVo.setAccessToken(Constant.accessToken.getAccess_token());
    	returnQrcodeVo.setJsonStr(jsonStr);
    	returnQrcodeVo.setUrl(a.getUrl());
    	returnQrcodeVo.setTicket(a.getTicket());
    	returnQrcodeVo.setEncodeTicket(URLEncoder.encode(a.getTicket())); 	
    	
    	return returnQrcodeVo;
	}
}
